package com.zb.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.entity.ZjKnowledgeType;
import com.zb.entity.ZjProductKnowledgeType;
import com.zb.goods.service.ConProductKnowledgeTypeService;
import com.zb.result.PageUtil;
import com.zb.service.ZjKnowledgeTypeService;
import com.zb.service.ZjKnowledgeTypeVideoService;
import com.zb.service.ZjProductKnowledgeTypeService;
import com.zb.service.ZjVideoService;
import com.zb.vo.ProductKnowledgeTypeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author WEN
 * @Date 2023/11/13 0013
 **/
@Service
public class ConProductKnowledgeTypeServiceImpl implements ConProductKnowledgeTypeService {
    @Resource
    private ZjProductKnowledgeTypeService productKnowledgeTypeService;
    @Resource
    private ZjKnowledgeTypeService knowledgeTypeService;
    @Resource
    private ZjKnowledgeTypeVideoService knowledgeTypeVideoService;
    @Resource
    private ZjVideoService videoService;

    // 根据产品ID获取视频
    @Override
    public Map<String, Object> getProductKnowledgeType(Long productId, PageUtil pageUtil) {
        PageUtil.setPageLimit(pageUtil);
        QueryWrapper<ZjProductKnowledgeType> productKnowledgeTypeQueryWrapper = new QueryWrapper<>();
        productKnowledgeTypeQueryWrapper.select("knowledge_type_id");
        productKnowledgeTypeQueryWrapper.eq("product_id", productId);
        Page<ZjProductKnowledgeType> page = productKnowledgeTypeService.page(new Page<>(pageUtil.getPage(), pageUtil.getLimit()), productKnowledgeTypeQueryWrapper);

        List<Long> knowledgeTypeIds = page.getRecords().stream().map(ZjProductKnowledgeType::getKnowledgeTypeId).distinct().toList();
        List<ZjKnowledgeType> knowledgeTypes = knowledgeTypeService.listByIds(knowledgeTypeIds);

        List<ProductKnowledgeTypeVo> voList = page.getRecords().stream().map(type -> {
            return new ProductKnowledgeTypeVo(type.getKnowledgeTypeId(),
                    knowledgeTypes.stream().filter(f -> Objects.equals(f.getId(), type.getKnowledgeTypeId())).map(ZjKnowledgeType::getName).findFirst().orElse("")
            );
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

}
